/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.basic.safe.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.basic.common.constant.FlowDicConstant;
import com.hopes.bizz.basic.drive.dto.BaseAuditDTO;
import com.hopes.bizz.basic.safe.dto.BaseLicenceProductionExcelDTO;
import com.hopes.bizz.basic.safe.dto.BaseProductionFlowSearchDTO;
import com.hopes.bizz.basic.safe.entity.BaseLicenceProductionEntity;
import com.hopes.bizz.basic.safe.service.BaseLicenceProductionService;
import com.hopes.bizz.basic.safe.vo.BaseLicenceProductionExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.core.util.StringUtils;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 安全生产许可证
 *
 * @author pig
 * @date 2023-11-01 16:44:49
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/production")
@Tag(description = "production", name = "安全生产许可证管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BaseLicenceProductionController {

    private final BaseLicenceProductionService baseLicenceProductionService;

    /**
     * 分页查询
     *
     * @param page                  分页对象
     * @param baseLicenceProduction 安全生产许可证
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('safe_production_view')")
    public R getbaseLicenceProductionPage(Page page, BaseLicenceProductionEntity baseLicenceProduction) {
        LambdaQueryWrapper<BaseLicenceProductionEntity> wrapper = Wrappers.lambdaQuery();
        wrapper
                .eq(ObjectUtil.isNotNull(baseLicenceProduction.getOrgId()), BaseLicenceProductionEntity::getOrgId, baseLicenceProduction.getOrgId())
                .ge(ObjectUtil.isNotNull(baseLicenceProduction.getEffectiveDate()), BaseLicenceProductionEntity::getEffectiveDate, baseLicenceProduction.getEffectiveDate())
                .le(ObjectUtil.isNotNull(baseLicenceProduction.getIneffectiveDate()), BaseLicenceProductionEntity::getIneffectiveDate, baseLicenceProduction.getIneffectiveDate())
                .eq(ObjectUtil.isNotNull(baseLicenceProduction.getLicenseStatus()), BaseLicenceProductionEntity::getLicenseStatus, baseLicenceProduction.getLicenseStatus())
                .like(StringUtils.isNotBlank(baseLicenceProduction.getLicenseName()), BaseLicenceProductionEntity::getLicenseName, baseLicenceProduction.getLicenseName())
                .orderByDesc(BaseLicenceProductionEntity::getOrgId);
        return R.ok(baseLicenceProductionService.page(page, wrapper));
    }

    /**
     * 分页查询
     *
     * @param page                  分页对象
     * @param baseLicenceProduction 安全生产许可证
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/declarePage")
    @PreAuthorize("@pms.hasPermission('safe_production_view')")
    public R declarePage(Page page, BaseLicenceProductionEntity baseLicenceProduction) {
        LambdaQueryWrapper<BaseLicenceProductionEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(baseLicenceProduction.getOrgId()), BaseLicenceProductionEntity::getOrgId, baseLicenceProduction.getOrgId())
                .like(StringUtils.isNotBlank(baseLicenceProduction.getLicenseName()), BaseLicenceProductionEntity::getLicenseName, baseLicenceProduction.getLicenseName())
                .and(item -> item.eq(BaseLicenceProductionEntity::getAuditStatus, FlowDicConstant.NOT_PASS).or().eq(BaseLicenceProductionEntity::getAuditStatus, FlowDicConstant.FILL_IN))
                .orderByDesc(BaseLicenceProductionEntity::getOrgId);
        ;
        return R.ok(baseLicenceProductionService.page(page, wrapper));
    }


    /**
     * 通过id查询安全生产许可证
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('safe_production_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(baseLicenceProductionService.getById(id));
    }

    /**
     * 新增安全生产许可证
     *
     * @param baseLicenceProduction 安全生产许可证
     * @return R
     */
    @Operation(summary = "新增安全生产许可证", description = "新增安全生产许可证")
    @SysLog("新增安全生产许可证")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('safe_production_add')")
    public R save(@Validated @RequestBody BaseLicenceProductionEntity baseLicenceProduction) {
        baseLicenceProduction.setAuditStatus(FlowDicConstant.FILL_IN);
        return R.ok(baseLicenceProductionService.save(baseLicenceProduction));
    }

    /**
     * 修改安全生产许可证
     *
     * @param baseLicenceProduction 安全生产许可证
     * @return R
     */
    @Operation(summary = "修改安全生产许可证", description = "修改安全生产许可证")
    @SysLog("修改安全生产许可证")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('safe_production_edit')")
    public R updateById(@RequestBody BaseLicenceProductionEntity baseLicenceProduction) {
        return R.ok(baseLicenceProductionService.updateById(baseLicenceProduction));
    }

    /**
     * 通过id删除安全生产许可证
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除安全生产许可证", description = "通过id删除安全生产许可证")
    @SysLog("通过id删除安全生产许可证")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('safe_production_del')")
    public R removeById(@RequestBody Long[] ids) {
        baseLicenceProductionService.deleteTask(CollUtil.toList(ids));
        return R.ok();
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('safe_production_export')")
    public List<BaseLicenceProductionExcelVO> template() {
        return baseLicenceProductionService.exportExcel(Collections.singletonList(new BaseLicenceProductionEntity()));
    }

    /**
     * 导出excel 表格
     *
     * @param baseLicenceProductionExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('safe_production_export')")
    public List<BaseLicenceProductionExcelVO> export(BaseLicenceProductionExcelDTO baseLicenceProductionExcelDto) {
        LambdaQueryWrapper<BaseLicenceProductionEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(baseLicenceProductionExcelDto.getOrgId()), BaseLicenceProductionEntity::getOrgId, baseLicenceProductionExcelDto.getOrgId())
                .like(StringUtils.isNotBlank(baseLicenceProductionExcelDto.getLicenseName()), BaseLicenceProductionEntity::getLicenseName, baseLicenceProductionExcelDto.getLicenseName());
        return baseLicenceProductionService.exportExcel(baseLicenceProductionService.list(wrapper));
    }

    /**
     * 导入excel 表格
     *
     * @param baseLicenceProductionExcelDtoList 查询条件
     * @param bindingResult                     错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('safe_production_export')")
    public R importExcel(@RequestExcel List<BaseLicenceProductionExcelDTO> baseLicenceProductionExcelDtoList, BindingResult bindingResult) {
        return baseLicenceProductionService.importExcel(baseLicenceProductionExcelDtoList, bindingResult);
    }

    @Operation(summary = "查询我的待审批", description = "查询我的待审批")
    @GetMapping("/queryTodoForPage")
    public R queryTodoForPage(Page page, BaseProductionFlowSearchDTO flowSearch) {
        flowSearch.setTaskKey(FlowDicConstant.IN_REVIEW);
        Page res = baseLicenceProductionService.queryTodoForPage(page, flowSearch);
        return R.ok(res);
    }

    @Operation(summary = "提交审批结果", description = "提交审批结果")
    @SysLog("提交审批结果")
    @PostMapping("/handlerAudit")
    public R handlerAudit(@Validated @RequestBody BaseAuditDTO baseAuditDTO) {
        baseLicenceProductionService.handlerAudit(baseAuditDTO);
        return R.ok();
    }

    @Operation(summary = "提交", description = "提交")
    @SysLog("提交")
    @GetMapping("/handleSubmit/{id}")
    public R handleSubmit(@PathVariable("id") Long id) {
        baseLicenceProductionService.handleSubmit(id);
        return R.ok();
    }
}
